package com.example.springbotorderrabbitmqproducer.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

//@Configuration
public class TTLDeadRabbitmqConfig {

    //声明 direct交换机
//    @Bean
    public DirectExchange ttlExchange() {
        return new DirectExchange("ttl-direct-exchange", true, false);
    }

    //声明 队列 sms.direct.queue email.direct.queue duanxin.direct.queue
//    @Bean
    public Queue ttlSmsQueue() {
        /*
         *设置死信队列（死信交换机），消息变为死信有以下三个原因：
         * 1.消息被拒绝
         * 2.消息过期
         * 3.队列达到最大长度
        * */
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 5000);
        args.put("x-dead-letter-exchange","dead-direct-exchange");
        args.put("x-dead-letter-routing-key","dead"); //如果是fanout 不需要配置
        return new Queue("ttl.direct.queue", true, false, false, args);
    }

    //完成 交换机和队列的绑定
//    @Bean
    public Binding ttlSmsBinding() {
        return BindingBuilder.bind(ttlSmsQueue()).to(ttlExchange()).with("ttl");
    }


}
